package net.learn.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.learn.system.service.QueSubjectService;
import net.learn.system.vo.QueSubjectVO;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.service.impl.BaseServiceImpl;
import net.learn.system.convert.QueSubjectConvert;
import net.learn.system.dao.QueSubjectDao;
import net.learn.system.entity.QueSubjectEntity;
import net.learn.system.query.QueSubjectQuery;
import net.learn.system.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理
 * 
 * @author 李立志
 */
@Service
@AllArgsConstructor
public class QueSubjectServiceImpl extends BaseServiceImpl<QueSubjectDao, QueSubjectEntity> implements QueSubjectService {

	@Override
	public PageResult<QueSubjectVO> page(QueSubjectQuery query) {
		IPage<QueSubjectEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

		return new PageResult<>(QueSubjectConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
	}

	@Override
	public List<QueSubjectVO> getList(QueSubjectQuery query) {
		List<QueSubjectEntity> entityList = baseMapper.selectList(getWrapper(query));

		return QueSubjectConvert.INSTANCE.convertList(entityList);
	}
	private Wrapper<QueSubjectEntity> getWrapper(QueSubjectQuery query){
		LambdaQueryWrapper<QueSubjectEntity> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StrUtil.isNotBlank(query.getSubjectName()), QueSubjectEntity::getSubjectName, query.getSubjectName());

		return wrapper;
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(QueSubjectVO vo) {
		QueSubjectEntity entity = QueSubjectConvert.INSTANCE.convert(vo);
		baseMapper.insert(entity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(QueSubjectVO vo) {
		QueSubjectEntity entity = QueSubjectConvert.INSTANCE.convert(vo);
		// 更新科目
		updateById(entity);
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(List<Long> idList) {
		// 删除科目
		removeByIds(idList);
	}
}